// pages/identification/identification.js
import {
  ididentify,
  moneylist,
  uploadimg
} from '../../api/identify'
import {
  getToken
} from '../../utils/storage'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    realname: "",
    idnumber: "",
    hostprice: "",
    hostaddress: "",
    IdPictempFilePaths: [],
    StagePictempFilePaths: [],
    region: [],
    customItem: '',
    showidentify: true,
    identifing: false,
    complete: false,
    rejected: false,
    agreeVisible: true,
    conditionOfUserSource5: '0'
  },
  gobackto() {
    wx.navigateBack({
      delta: 1,
    })
  },
  bindRegionChange: function (e) {

    this.setData({
      region: e.detail.value,
      hostaddress: e.detail.value
    })
  },
  deletestagepic(e) {
    let i = parseInt(e.target.id, 10);

    let arr = this.data.StagePictempFilePaths.splice(i, 1);


    this.setData({
      StagePictempFilePaths: this.data.StagePictempFilePaths,
    })
  },
  deleteidpic(e) {
    this.setData({
      ['IdPictempFilePaths[' + e.target.id + ']']: ""
    });
    if (this.data.IdPictempFilePaths[0] == "" && this.data.IdPictempFilePaths[1] == "") {
      this.setData({
        IdPictempFilePaths: []
      })
    }
  },
  stageImg(e) {
    let ee = e.target.id
    let temparrlength = this.data.StagePictempFilePaths.length
    if (temparrlength < 3) {
      wx.chooseImage({
        count: 3 - temparrlength,
        sizeType: ['original'],
        sourceType: ['album', 'camera'],
        success: (res) => {

          const tmpToken = getToken();
          const {
            tempFilePaths
          } = res
          for (let i = 0; i < tempFilePaths.length; i++) {
            wx.uploadFile({
              filePath: tempFilePaths[i],
              name: 'file',
              url: 'https://www.hudongmiao.com/beiJing/shangchuanTomcat',
              header: {
                'token': tmpToken,
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              success: (res) => {
                let v = ee++;

                let webidpic = JSON.parse(res.data);
                this.setData({
                  ['StagePictempFilePaths[' + v + ']']: webidpic.data.filePath
                })

              },
              fail: function (res) {

              }
            })
          }

        }
      })

    }

  },
  idImg(e) {
    let ee = e.target.id;
    wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      sourceType: ['album', 'camera'],
      success: (res) => {

        const tmpToken = getToken();
        const {
          tempFilePaths
        } = res;
        // let tempath = {}

        wx.uploadFile({
          filePath: tempFilePaths[0],
          name: 'file',
          url: 'https://www.hudongmiao.com/beiJing/shangchuanTomcat',
          header: {
            'token': tmpToken,
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: (res) => {

            let webidpic = JSON.parse(res.data);
            this.setData({
              ['IdPictempFilePaths[' + ee + ']']: webidpic.data.filePath
            })
          }
        })
      }
    })
  },
  backtoidentity: function () {
    this.setData({
      showidentify: true,
      identifing: false,
      complete: false,
      rejected: false
    })
  },
  addsubmitbtn: function (e) {
    let id_card_photo = this.data.IdPictempFilePaths.toString();
    let stage_photo = this.data.StagePictempFilePaths.toString() ? this.data.StagePictempFilePaths.toString() : 0;;
    let b = e.detail.value;
    let real_name = b.realname ? b.realname : 0;
    let id_card = b.idnum ? b.idnum : 0;
    let price = b.hostprice ? b.hostprice : 0;
    let common_address = b.placeaddress ? b.placeaddress : 0;
    let auth_state = "1";;
    let i = e.detail.value
    // ;
    if (!i.realname) {
      wx.showToast({
        title: '请填写您的真实姓名',
        icon: 'none'
      })
      return
    } else if (!i.idnum) {
      wx.showToast({
        title: '请填写您的身份证号码',
        icon: 'none'
      })
      return
    } else if (i.idnum.length < 18) {
      wx.showToast({
        title: '请输入正确的身份证号码',
        icon: 'none'
      })
      return
    } else if (!i.hostprice) {
      wx.showToast({
        title: '请输入主持价格',
        icon: 'none'
      })
      return
    } else if (!i.placeaddress) {
      wx.showToast({
        title: '请选择您的常驻地址',
        icon: 'none'
      })
      return
    } else if (this.data.IdPictempFilePaths.length !== 2) {
      wx.showToast({
        title: '请上传身份证照片',
        icon: 'none'
      })
      return
    } else if (this.data.IdPictempFilePaths[0] == "") {
      wx.showToast({
        title: '请上传身份证正面照片',
        icon: 'none'
      })
      return
    } else if (this.data.IdPictempFilePaths[1] == "") {
      wx.showToast({
        title: '请上传身份证反面照片',
        icon: 'none'
      })
      return
    } else if (this.data.StagePictempFilePaths.length !== 3) {
      wx.showToast({
        title: '请上传3张舞台照',
        icon: 'none'
      })
      return
    }
    ididentify({
      real_name,
      id_card,
      price,
      common_address,
      id_card_photo,
      stage_photo,
      auth_state
    }).then((res) => {

      if (res.data === "success") {
        wx.showToast({
          title: '上传成功',
          success: () => {
            this.setData({
              showidentify: false,
              identifing: true,
              complete: false
            })
          }
        })
      }
    })
  },
  handleContinueEvent() {
    this.setData({
      agreeVisible: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // user-source:5 注册渠道跳转过来的情况
    this.setData({
      conditionOfUserSource5: options.conditionOfUserSource5 || '0'
    });
    moneylist({
      currentPage: 1,
      showCount: 1
    }).then((res) => {

      let i = []
      if (res.data.user && res.data.user.id_card_photo) {
        i = res.data.user.id_card_photo.split(",")
      }
      let b = []
      if (res.data.user && res.data.user.stage_photo === "0") {
        b = (res.data.user && res.data.user.stage_photo) ? res.data.user.stage_photo.split(",") : []
      }
      if (res.data.user.auth_state == "0") {
        this.setData({
          showidentify: true,
          identifing: false,
          complete: false,
          rejected: false
        })
      } else if (res.data.user.auth_state == "1") {
        this.setData({
          showidentify: false,
          identifing: true,
          complete: false,
          rejected: false
        })
      } else if (res.data.user.auth_state == "2") {
        this.setData({
          showidentify: false,
          identifing: false,
          complete: true,
          rejected: false
        })
      } else if (res.data.user.auth_state == "3") {
        this.setData({
          showidentify: false,
          identifing: false,
          complete: false,
          rejected: true
        })
      }
      this.setData({
        realname: res.data.user.real_name == 0 ? "" : res.data.user.real_name,
        idnumber: res.data.user.id_card == 0 ? "" : res.data.user.id_card,
        hostprice: res.data.user.price == 0 ? "" : res.data.user.price,
        hostaddress: res.data.user.common_address == 0 ? "" : res.data.user.common_address,
        IdPictempFilePaths: i,
        StagePictempFilePaths: b,
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    // getidentify().then((res)=>{
    //    
    // })
    // let i = "wxfile://tmp_6ef5e4946a7d67d2d81d1552a78ba2ca.jpg";
    // let reader = new FileReader();
    // reader.readAsDataURL(i)
    // reader.onload = function() {
    //   ;
    //   uploadimg({this.result}).then((res)=>{
    //      

    //   })
    // }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})